<?php
class CommentModel extends Model {
	public function __construct() {
		parent::__construct('Comment');
	}
	
	public function getOneByPk($id, $field = '*') {
		return $this->field($field)->where('id='.$id)->find();
	}
	
	public function addOne(& $data) {
		return $this->data($data)->add();
	}
	
	public function deleteByCondition($where) {
		return $this->where($where)->delete();
	}
	
	public function getByCondition($where) {
		return $this->where($where)->select();
	}
	
	// 后台列表倒叙排列
	public function getListByPage($pageSize = 10) {
		import('ORG.Util.Page');
		$pre = C('DB_PREFIX');
		
		$count = $this->where('pid=0')->count();	
		if(empty($count)) $count = 0;
		$page = new Page($count, $pageSize);
		$pageString = $page->show();
		
		$sql = "select l.id,l.nickname,l.email,l.website,l.content,l.tongguo,l.posttime,r.title,(select count(*) from {$pre}comment as tbl where tbl.pid=l.id) as c".
			" from {$pre}comment l left join {$pre}article r on l.articleid=r.id".
			" where l.pid=0 and l.id<=(select id from {$pre}comment order by id desc limit {$page->firstRow},1) order by l.id desc limit {$pageSize}";
			
		$data = $this->query($sql);
		//var_dump($this->getLastsql()); exit;
		
		$data['pageString'] = $pageString;
		
		return $data;
	}
	
}